package com.jack;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Vector;

public class CounterItem {

    private String name;
    private int counter;
    private int sequence;
    private String imagePath;
    private boolean fullSceenMode;
    /*
     * count key,seperated by ;
     */
    private String countKeys;
    /*
     * any key to count
     */
    private boolean anyKeyCount;
    /*
     * background color
     * default: white
     */
    private int backColor = 0xffffff;
    /*
     * fore color
     * default: black
     */
    private int foreColor = 0x000000;
    /*
     * show image
     */
    private boolean showImage;
    private boolean keepScale = true;
    private int recordId;

    public CounterItem() {
        this.name = "";
        this.counter = 0;
        this.sequence = 0;
        this.imagePath = "";
        this.countKeys = "";
    }

    public CounterItem(String name, int count) {
        this.name = name;
        this.counter = count;
        this.sequence = 0;
        this.imagePath = "";
        this.countKeys = "";
    }

    public CounterItem(String name, int count, int seq) {
        this.name = name;
        this.counter = count;
        this.sequence = seq;
        this.imagePath = "";
        this.countKeys = "";
    }

    public CounterItem(String name, int count, int seq, String image) {
        this.name = name;
        this.counter = count;
        this.sequence = seq;
        this.imagePath = image;
        this.countKeys = "";
    }

    /*
     * sequence,name,value,imagepath,
     * forecolor,backage-color
     * anykey,fullscreen,neg-color
     */
    public CounterItem(byte[] data) throws IOException {
        ByteArrayInputStream bais = new ByteArrayInputStream(data);
        DataInputStream dis = new DataInputStream(bais);

        sequence = dis.readInt();
        name = dis.readUTF();
        counter = dis.readInt();
        imagePath = dis.readUTF();

        foreColor = dis.readInt();
        backColor = dis.readInt();

        anyKeyCount = dis.readBoolean();
        fullSceenMode = dis.readBoolean();
        showImage = dis.readBoolean();
        keepScale = dis.readBoolean();

        countKeys = dis.readUTF();


        bais.close();
        dis.close();
    }

    /*
     * sequence,name,value,imagepath
     */
    public byte[] getBytes() throws IOException {
        byte[] data = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        //dos.writeUTF(this.toString());

        dos.writeInt(sequence);
        dos.writeUTF(name);
        dos.writeInt(getCounter());
        dos.writeUTF(imagePath);

        dos.writeInt(foreColor);
        dos.writeInt(backColor);

        dos.writeBoolean(anyKeyCount);
        dos.writeBoolean(fullSceenMode);
        dos.writeBoolean(showImage);
        dos.writeBoolean(keepScale);

        dos.writeUTF(countKeys);

        data = baos.toByteArray();
        baos.close();
        dos.close();
        return data;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public int getSequence() {
        return sequence;
    }

    public void setSequence(int sequence) {
        this.sequence = sequence;
    }

    /**
     * @return the imagePath
     */
    public String getImagePath() {
        return imagePath;
    }

    /**
     * @param imagePath the imagePath to set
     */
    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }

    public String toString() {
        return this.name + ":" + this.getCounter() + ":" + this.sequence + ":" + this.imagePath + ":" + this.countKeys;
    }

    /**
     * @return the counter
     */
    public synchronized int getCounter() {
        return counter;
    }

    /**
     * @param counter the counter to set
     */
    public void setCounter(int counter) {
        this.counter = counter;
    }

    /**
     * @return the fullSceenMode
     */
    public boolean isFullSceenMode() {
        return fullSceenMode;
    }

    /**
     * @param fullSceenMode the fullSceenMode to set
     */
    public void setFullSceenMode(boolean fullSceenMode) {
        this.fullSceenMode = fullSceenMode;
    }

    /**
     * @return the countKeys
     */
    public String getCountKeys() {
        return countKeys;
    }

    public Vector getCountKeysVector(char sepChar) {
        Vector pressedKeys = new Vector();
        char[] chars = countKeys.toCharArray();
        for (int i = 0; i < chars.length; i++) {
            Character ch = new Character(chars[i]);
            if (chars[i] != sepChar && !pressedKeys.contains(ch)) {
                pressedKeys.addElement(ch);
            }
        }
        return pressedKeys;
    }

    /**
     * @param countKeys the countKeys to set
     */
    public void setCountKeys(String countKeys) {
        this.countKeys = countKeys;
    }

    /**
     * @return the anyKeyCount
     */
    public boolean isAnyKeyCount() {
        return anyKeyCount;
    }

    /**
     * @param anyKeyCount the anyKeyCount to set
     */
    public void setAnyKeyCount(boolean anyKeyCount) {
        this.anyKeyCount = anyKeyCount;
    }

    /**
     * @return the backColor
     */
    public int getBackColor() {
        return backColor;
    }

    /**
     * @param backColor the backColor to set
     */
    public void setBackColor(int backColor) {
        this.backColor = backColor;
    }

    /**
     * @return the negativeColor
     */
    public boolean isShowImage() {
        return showImage;
    }

    /**
     * @param negativeColor the negativeColor to set
     */
    public void setShowImage(boolean negativeColor) {
        this.showImage = negativeColor;
    }

    /**
     * @return the recordId
     */
    public int getRecordId() {
        return recordId;
    }

    /**
     * @param recordId the recordId to set
     */
    public void setRecordId(int recordId) {
        this.recordId = recordId;
    }

    public synchronized void Add() {
        counter = counter + 1;
    }

    public synchronized void Clear() {
        counter = 0;
    }

    /**
     * @return the foreColor
     */
    public int getForeColor() {
        return foreColor;
    }

    /**
     * @param foreColor the foreColor to set
     */
    public void setForeColor(int foreColor) {
        this.foreColor = foreColor;
    }

    /**
     * @return the keepScale
     */
    public boolean isKeepScale() {
        return keepScale;
    }

    /**
     * @param keepScale the keepScale to set
     */
    public void setKeepScale(boolean keepScale) {
        this.keepScale = keepScale;
    }
}
